package me.firstapp.service.weixin.system.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.weixin.system.Banner;
import me.firstapp.repository.weixin.system.BannerRepository;
import me.firstapp.service.weixin.system.BannerService;

@Service
public class BannerServiceImpl implements BannerService {

	@Autowired
	private BannerRepository bannerRepository;

	@Override
	public Page<Banner> findBannerPage(Integer pageNo, Integer pageSize, Map<String, Object> searchParams,
			String sortParam, String sortType) {
		return bannerRepository.findBannerPage(pageNo, pageSize, searchParams, sortParam, sortType);
	}

	@Override
	public Banner executeAdd(String tag, String name, String imageUrl, String url, String remark, Integer sort) {
		Banner banner = new Banner();
		banner.setTag(tag);
		banner.setName(name);
		banner.setImageUrl(imageUrl);
		banner.setUrl(url);
		banner.setRemark(remark);
		banner.setSort(sort);
		banner.setStatus(Banner.STATUS_OFF);
		bannerRepository.save(banner);
		return banner;
	}

	@Override
	public Banner findById(Long id) {
		return (Banner) bannerRepository.getById(id);
	}

	@Override
	public Banner executeUpdate(Long id, String tag, String name, String imageUrl, String url, String remark,
			Integer sort, Integer status) {
		Banner banner = (Banner) bannerRepository.getById(id);
		if (null == banner) {
			throw new ServiceException(StatusHouse.COMMON_PARAMETER_ERROR);
		}
		if (StrUtils.isNotNULL(tag)) {
			banner.setTag(tag);
		}
		if (StrUtils.isNotNULL(name)) {
			banner.setName(name);
		}
		if (StrUtils.isNotNULL(imageUrl)) {
			banner.setImageUrl(imageUrl);
		}
		if (StrUtils.isNotNULL(url)) {
			banner.setUrl(url);
		}
		if (StrUtils.isNotNULL(remark)) {
			banner.setRemark(remark);
		}
		if (null != sort) {
			banner.setSort(sort);
		}
		if (null != status) {
			banner.setStatus(status);
		}
		bannerRepository.update(banner);
		return banner;
	}

	@Override
	public List<Banner> findByTag(String tag) {
		return bannerRepository.findByTag(tag);
	}

}
